//
//  FeedbackVC.m
//  Cunli
//
//  Created by lilubin on 15/6/29.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "FeedbackVC.h"
#import "MyDataService.h"
#import "Config.h"
#import "NSString+MD5.h"
#import "MBProgressHUD.h"

@interface FeedbackVC () <UITextViewDelegate>

//@property (weak, nonatomic) IBOutlet UILabel *HeadingLabel;         //标题label
//@property (weak, nonatomic) IBOutlet UITextField *HeadingTextfield; //标题输入框
//@property (weak, nonatomic) IBOutlet UILabel *ContentLabel;         //内容label
@property (weak, nonatomic) IBOutlet UITextView *ContentTextView;   //内容输入框
@property (weak, nonatomic) IBOutlet UILabel *preInstructLabel;

@end

@implementation FeedbackVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setAllUIKitAttributes];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)setAllUIKitAttributes
{
    self.title = @"反馈";
    
    [_ContentTextView.layer setBorderColor:[UIColor colorWithHexString:TextViewBorderColorValue].CGColor];
    [_ContentTextView.layer setBorderWidth:0.5];
    [_ContentTextView.layer setCornerRadius:2.0];
    _ContentTextView.delegate = self;
    
//    [_HeadingTextfield.layer setBorderColor:[UIColor lightGrayColor].CGColor];
//    [_HeadingTextfield.layer setBorderWidth:0.5];
//    [_HeadingTextfield.layer setCornerRadius:7];
//    [_ContentTextView setBackgroundColor:[UIColor colorWithHexString:UIBgColorValue]];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 65, 30);
//    [rightBtn setBackgroundImage:[UIImage imageNamed:@"regnext"] forState:UIControlStateNormal];
    [rightBtn setTitle:@"确认" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = flexSpacer.width - 20;
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:flexSpacer, rightBarItem, nil]];
    [_preInstructLabel setTextColor:[UIColor colorWithHexString:@"0x8f8f8f"]];
}

//提交按钮完成的方法
- (void)commitAction:(UIButton *)button
{
    [self.view endEditing:YES];
    
    
    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:4];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:@"反馈" forKey:@"title"];
    [CheckParams setObject:_ContentTextView.text forKey:@"content"];
    
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    if (_ContentTextView.text.length != 0) {
        [self.navigationController popViewControllerAnimated:YES];
        [MyDataService requestURL:@"feedback.php" httpMethod:@"POST" params:CheckParams fileDatas:nil complection:^(id result) {
                NSLog(@"Feedback Result:%@", result);
                MBProgressHUD *hud = [Config createHUD];
                hud.mode = MBProgressHUDModeCustomView;
                NSString *response = result[@"response"];
                if ([response isEqualToString:@"feedback"]) {
                    //            NSLog(@"Report success");
                    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
                    hud.labelText = @"反馈成功";
                }
                else
                {
                    NSDictionary *errorDic = [result objectForKey:@"error"];
                    NSString *errorStr = [errorDic objectForKey:@"text"];
                    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
                    hud.labelText = errorStr;
                    
                    return;
                }
                [hud hide:YES afterDelay:1];
            } failureAction:^{
                MBProgressHUD *hud = [Config createHUD];
                hud.mode = MBProgressHUDModeCustomView;
                hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
                hud.labelText = @"反馈失败";
                [hud hide:YES afterDelay:1];
            }];
    }
    else
    {
        MBProgressHUD *hud = [Config createHUD];
        hud.mode = MBProgressHUDModeCustomView;
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
        hud.labelText = @"请输入反馈内容";
        [hud hide:YES afterDelay:1];

    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    _preInstructLabel.hidden = YES;
    return YES;
}

@end
